<?php
namespace Rainrock\Framework\kernel;

use Rainrock\Framework\kernel\base\Router;
use Rainrock\Framework\kernel\base\Base;
use Rainrock\Framework\kernel\base\Rock;
use Rainrock\Framework\kernel\core\Aock;

class Start{
	
	/**
	* 开始运行
	* @param $path 系统根路径
	* @param $package 主包名
	* @param $packpath 主包名的路径
	*/
	public static function run($path, $package= '', $packpath='')
	{
		Base::runStart($path, $package, $packpath);
		$barr = self::runs();
		if(is_array($barr))$barr = json_encode($barr);
		if(is_object($barr) && method_exists($barr, 'show'))$barr = $barr->show();
		Base::runEnd();
		if(!Rock::isempt($barr))echo $barr;
	}
	
	/**
	* 运行
	*/
	private static function runs()
	{
		$router 		= Router::get();
		$dir 			= $router['dir'];
		$controlller 	= $router['controlller'];
		$actios 		= $router['action'];
		$action 		= $actios.'Action';
		$obj			= Aock::getController($dir, $controlller);
		$barr			= '';
		define('ACTION', $actios);
		define('CONTROLLER', $controlller);
		if(method_exists($obj, $action)){
			$obj->initController();
			$barr = $obj->checkAccess($actios);
			if(!$barr)$barr = $obj->$action();
		}else{
			$barr = '('.$controlller.')action('.$action.') not found';
		}
		$obj->onControllerAfter($barr);
		return $barr;
	}
}